package com.itheima.service.impl;

import cn.hutool.core.util.IdUtil;
import com.itheima.mapper.BlacklistMapper;
import com.itheima.pojo.Blacklist;
import com.itheima.service.BlacklistService;
import com.itheima.thread.UserThreadLocal;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author wangjiexin
 */
@Service
@Slf4j
public class BlacklistServiceImpl implements BlacklistService {
    @Autowired
    BlacklistMapper blacklistMapper;

    @Override
    public boolean add(Blacklist blacklist) {
        //入参校验
            if (blacklist==null|| StringUtils.isEmpty(blacklist.getBlacklistId())){
                throw new RuntimeException("入参不能为空~！！");
            }
        //获取当前线程的id和对方的id
        String userId = UserThreadLocal.get();

        String blacklistId = blacklist.getBlacklistId();
        //生成黑名单id
        String id = IdUtil.getSnowflakeNextIdStr();
        //把黑名单对象添加到数据库
        //返回结果

        return blacklistMapper.add(id,userId,blacklistId);
    }

    @Override
    public boolean delete(String id) {
        //删除黑名单
        if (id==null||StringUtils.isEmpty(id)){
            throw new RuntimeException("入参不能为空");
        }

        String userId = UserThreadLocal.get();


        return blacklistMapper.delete(id,userId);
    }
}
